package com.yazi.mxz.mobile.controller.user;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.ComSetting;
import com.yazi.mxz.common.entity.beans.SecurityQuestion;
import com.yazi.mxz.common.entity.enums.SettingId;
import com.yazi.mxz.common.service.SecurityQuestionService;
import com.yazi.mxz.mobile.common.VerifyAbstractController;

@Controller("user-securityQuestionController")
@RequestMapping("/user/securityQuestion")
public class SecurityQuestionController extends VerifyAbstractController {
	private static Logger log = LoggerFactory.getLogger(SecurityQuestionController.class);
	
	@Resource
	SecurityQuestionService securityQuestionService;
	
	@RequestMapping
	public String updateQuestion() {
		log.debug("进入安全问题设置页面");
		return "v1.5/user/securityQuestion";
	}
	
	@RequestMapping("/preset/json")
	@ResponseBody
	public String presetJson() {		
		return ComSetting.getValue(SettingId.SECURITY_QUESTION);
	}
	
	@RequestMapping("/do/json")
	@ResponseBody
	public StatusResult doJson(QuestionsFromForm questionsFromForm) {
		log.debug("保存安全问题");
		StatusResult jsonResult = new StatusResult();
		List<SecurityQuestion> sqList = questionsFromForm.getSqList();
		try {
			securityQuestionService.addByMember(getMember(), sqList);
			
			jsonResult.setOk();
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			log.error("保存安全问题异常:", e);
			jsonResult.setErrorMessage("保存失败，请联系管理员");
		}
		return jsonResult;
	}
}

class QuestionsFromForm {
	private List<SecurityQuestion> sqList;

	public List<SecurityQuestion> getSqList() {
		return sqList;
	}

	public void setSqList(List<SecurityQuestion> sqList) {
		this.sqList = sqList;
	}
}